﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace EnterpriseFund.DAL
{
    /// <summary>
    /// 数据访问类:WorkFlowProcess
    /// </summary>
    public partial class WorkFlowProcess
    {
        public WorkFlowProcess()
        { }
        #region  BasicMethod

        public List<string> GetMinMaxNo(string FTableName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT MAX(FSeq) AS maxSeq,MIN(FSeq) AS minSeq FROM fund.t_WorkFlowDefine WHERE FTableName=@FTableName ");
            SqlParameter[] parameters = {
					new SqlParameter("@FTableName", SqlDbType.NVarChar,30)
			};
            parameters[0].Value = FTableName;
            List<string> strList = new List<string>();

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                strList.Add(ds.Tables[0].Rows[0]["maxSeq"].ToString());
                strList.Add(ds.Tables[0].Rows[0]["minSeq"].ToString());
               
            }
            return strList;
        }

        public string GetLastNo(string FTableName, string FSeq)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  isnull(MAX(FSeq),-1) AS minSeq FROM fund.t_WorkFlowDefine WHERE FTableName=@FTableName and FSeq<" + FSeq);
            SqlParameter[] parameters = {
					new SqlParameter("@FTableName", SqlDbType.NVarChar,30)
			};
            parameters[0].Value = FTableName;
            string strList = "";

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                strList = ds.Tables[0].Rows[0]["minSeq"].ToString();
            }
            return strList;
        }

        public string GetTaxCheckIssName(string no)
        { 
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"   SELECT * FROM (
  SELECT a.FEmplNo,b.FEmpName,MIN(a.FSeq) AS FSeq FROM fund.t_WorkFlowProcess AS a LEFT JOIN dbo.t_HREmpl b ON a.FEmplNo = b.FEmplNo
  WHERE FTableName ='t_TaxAuditForm' AND FSourceNo IN ( 
  SELECT FTaxAuditId FROM fund.t_TaxAuditPolicyEval WHERE FIssueID='" + no + @"' ) 
  GROUP BY a.FEmplNo,FEmpName
  ) AS a ORDER BY a.FSeq");
            string strList = "";

            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (strList != "")
                    {
                        strList += "," + dr["FEmpName"].ToString();
                    }
                    else
                    {
                        strList += dr["FEmpName"].ToString();
                    }
                }
            }
            return strList;
        }

        public string GetCHekIssName(string no)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"  SELECT DISTINCT a.FEmplNo,b.FEmpName FROM fund.t_WorkFlowProcess AS a LEFT JOIN dbo.t_HREmpl b ON a.FEmplNo = b.FEmplNo
  WHERE FTableName ='t_AllowanceIssue' AND FSourceNo='" + no + "' AND a.FSeq='20'");
            string strList = "";

            DataSet ds = DbHelperSQL.Query(strSql.ToString());


            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (strList != "")
                    {
                        strList += "," + dr["FEmpName"].ToString();
                    }
                    else
                    {
                        strList += dr["FEmpName"].ToString();
                    }
                }
            }
            return strList;
        }

        public string GetNextNo(string FTableName, string FSeq)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  isnull(MIN(FSeq),-1) AS minSeq FROM fund.t_WorkFlowDefine WHERE FTableName=@FTableName and FSeq>" + FSeq);
            SqlParameter[] parameters = {
					new SqlParameter("@FTableName", SqlDbType.NVarChar,30)
			};
            parameters[0].Value = FTableName;
            string strList ="";

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                strList = ds.Tables[0].Rows[0]["minSeq"].ToString();
            }
            return strList;
        }

        public bool UpdateR(string no, string key, string FStatus,string tableName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update fund." + tableName + " set ");
            strSql.Append("FIsReturn='Y' ");
            if (FStatus != null && FStatus != "")
            {
                strSql.Append(",FStatus='" + FStatus + "'");
            }
            strSql.Append(" where " + key + "='" + no + "'  ");

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(EnterpriseFund.Model.WorkFlowProcess model, EnterpriseFund.Model.WorkFlowToDo wtModel)
        {
            StringBuilder strSql = new StringBuilder();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            strSql.Append("insert into fund.t_WorkFlowProcess(");
            strSql.Append("FEntityNo,FTableName,FSourceNo,FSeq,FStatus,FReturnToFlowProcID,FNote,FDeptNo,FEmplNo,FProcDate,FInsIP)");
            strSql.Append(" values (");
            strSql.Append("@FEntityNo,@FTableName,@FSourceNo,@FSeq,@FStatus,@FReturnToFlowProcID,@FNote,@FDeptNo,@FEmplNo,@FProcDate,@FInsIP)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FTableName", SqlDbType.NVarChar,30),
					new SqlParameter("@FSourceNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FSeq", SqlDbType.Int,4),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1),
					new SqlParameter("@FReturnToFlowProcID", SqlDbType.Decimal,13),
					new SqlParameter("@FNote", SqlDbType.NVarChar,255),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FProcDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FEntityNo;
            parameters[1].Value = model.FTableName;
            parameters[2].Value = model.FSourceNo;
            parameters[3].Value = model.FSeq;
            parameters[4].Value = model.FStatus;
            parameters[5].Value = model.FReturnToFlowProcID;
            parameters[6].Value = model.FNote;
            parameters[7].Value = model.FDeptNo;
            parameters[8].Value = model.FEmplNo;
            parameters[9].Value = model.FProcDate;
            parameters[10].Value = model.FInsIP;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            cmds = new CommandInfo();
            strSql = new StringBuilder();
            strSql.Append("update fund.t_WorkFlowToDo set ");
            strSql.Append("FSourceNo=@FSourceNo,");
            strSql.Append("FStatus=@FStatus");
            strSql.Append(" where FTableName=@FTableName and FSourceNo=@FSourceNo and FSeq=@FSeq ");
            SqlParameter[] parameters3 = {
					new SqlParameter("@FSourceNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FStatus", SqlDbType.Char,1),
					new SqlParameter("@FTableName", SqlDbType.NVarChar,30),
					new SqlParameter("@FSeq", SqlDbType.Int,4)};
            parameters3[0].Value = model.FSourceNo;
            parameters3[1].Value = "Y";
            parameters3[2].Value = model.FTableName;
            parameters3[3].Value = model.FSeq;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters3;
            cmdList.Add(cmds);

            if (wtModel != null)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into fund.t_WorkFlowToDo(");
                strSql.Append("FEntityNo,FTableName,FSeq,FSourceNo,FSubmitDeptNo,FSubmitEmplNo,FSubmitDate,FSubmitIP,FStatus)");
                strSql.Append(" values (");
                strSql.Append("@FEntityNo,@FTableName,@FSeq,@FSourceNo,@FSubmitDeptNo,@FSubmitEmplNo,@FSubmitDate,@FSubmitIP,@FStatus)");
                strSql.Append(";select @@IDENTITY");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FTableName", SqlDbType.NVarChar,30),
					new SqlParameter("@FSeq", SqlDbType.Int,4),
					new SqlParameter("@FSourceNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FSubmitDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FSubmitEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FSubmitDate", SqlDbType.DateTime),
					new SqlParameter("@FSubmitIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FStatus", SqlDbType.Char,1)};
                parameters2[0].Value = wtModel.FEntityNo;
                parameters2[1].Value = wtModel.FTableName;
                parameters2[2].Value = wtModel.FSeq;
                parameters2[3].Value = wtModel.FSourceNo;
                parameters2[4].Value = wtModel.FSubmitDeptNo;
                parameters2[5].Value = wtModel.FSubmitEmplNo;
                parameters2[6].Value = wtModel.FSubmitDate;
                parameters2[7].Value = wtModel.FSubmitIP;
                parameters2[8].Value = wtModel.FStatus;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }


            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithCommonList(cmdList);
            }

        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void AddMessage(EnterpriseFund.Model.WorkFlowProcess model, EnterpriseFund.Model.WorkFlowToDo wtModel)
        {
            StringBuilder strSql = new StringBuilder();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            strSql.Append("insert into fund.t_WorkFlowProcess(");
            strSql.Append("FEntityNo,FTableName,FSourceNo,FSeq,FStatus,FReturnToFlowProcID,FNote,FDeptNo,FEmplNo,FProcDate,FInsIP)");
            strSql.Append(" values (");
            strSql.Append("@FEntityNo,@FTableName,@FSourceNo,@FSeq,@FStatus,@FReturnToFlowProcID,@FNote,@FDeptNo,@FEmplNo,@FProcDate,@FInsIP)");
            strSql.Append(";SELECT @returnid = IDENT_CURRENT('fund.t_WorkFlowProcess')");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FTableName", SqlDbType.NVarChar,30),
					new SqlParameter("@FSourceNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FSeq", SqlDbType.Int,4),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1),
					new SqlParameter("@FReturnToFlowProcID", SqlDbType.Decimal,13),
					new SqlParameter("@FNote", SqlDbType.NVarChar,255),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FProcDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@returnid", SqlDbType.Decimal,15)};
            parameters[0].Value = model.FEntityNo;
            parameters[1].Value = model.FTableName;
            parameters[2].Value = model.FSourceNo;
            parameters[3].Value = model.FSeq;
            parameters[4].Value = model.FStatus;
            parameters[5].Value = model.FReturnToFlowProcID;
            parameters[6].Value = model.FNote;
            parameters[7].Value = model.FDeptNo;
            parameters[8].Value = model.FEmplNo;
            parameters[9].Value = model.FProcDate;
            parameters[10].Value = model.FInsIP;
            parameters[11].Direction = ParameterDirection.Output;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            if (wtModel != null)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into fund.t_WorkFlowToDo(");
                strSql.Append("FEntityNo,FTableName,FSeq,FSourceNo,FSubmitDeptNo,FSubmitEmplNo,FSubmitDate,FSubmitIP,FStatus)");
                strSql.Append(" values (");
                strSql.Append("@FEntityNo,@FTableName,@FSeq,@returnid,@FSubmitDeptNo,@FSubmitEmplNo,@FSubmitDate,@FSubmitIP,@FStatus)");
                strSql.Append(";select @@IDENTITY");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FTableName", SqlDbType.NVarChar,30),
					new SqlParameter("@FSeq", SqlDbType.Int,4),
					new SqlParameter("@returnid", SqlDbType.NVarChar,50),
					new SqlParameter("@FSubmitDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FSubmitEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FSubmitDate", SqlDbType.DateTime),
					new SqlParameter("@FSubmitIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FStatus", SqlDbType.Char,1)};
                parameters2[0].Value = wtModel.FEntityNo;
                parameters2[1].Value = wtModel.FTableName;
                parameters2[2].Value = wtModel.FSeq;
                parameters2[3].Direction = ParameterDirection.InputOutput;
                parameters2[4].Value = wtModel.FSubmitDeptNo;
                parameters2[5].Value = wtModel.FSubmitEmplNo;
                parameters2[6].Value = wtModel.FSubmitDate;
                parameters2[7].Value = wtModel.FSubmitIP;
                parameters2[8].Value = wtModel.FStatus;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithCommonList(cmdList);
            }

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.WorkFlowProcess GetModel(decimal FFlowProcID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FFlowProcID,FEntityNo,FTableName,FSourceNo,FSeq,FStatus,FReturnToFlowProcID,FNote,FDeptNo,FEmplNo,FProcDate,FInsIP from fund.t_WorkFlowProcess ");
            strSql.Append(" where FFlowProcID=@FFlowProcID");
            SqlParameter[] parameters = {
					new SqlParameter("@FFlowProcID", SqlDbType.Decimal)
			};
            parameters[0].Value = FFlowProcID;

            EnterpriseFund.Model.WorkFlowProcess model = new EnterpriseFund.Model.WorkFlowProcess();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.WorkFlowProcess DataRowToModel(DataRow row)
        {
            EnterpriseFund.Model.WorkFlowProcess model = new EnterpriseFund.Model.WorkFlowProcess();
            if (row != null)
            {
                if (row["FFlowProcID"] != null && row["FFlowProcID"].ToString() != "")
                {
                    model.FFlowProcID = decimal.Parse(row["FFlowProcID"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FTableName"] != null)
                {
                    model.FTableName = row["FTableName"].ToString();
                }
                if (row["FSourceNo"] != null)
                {
                    model.FSourceNo = row["FSourceNo"].ToString();
                }
                if (row["FSeq"] != null && row["FSeq"].ToString() != "")
                {
                    model.FSeq = int.Parse(row["FSeq"].ToString());
                }
                if (row["FStatus"] != null)
                {
                    model.FStatus = row["FStatus"].ToString();
                }
                if (row["FReturnToFlowProcID"] != null && row["FReturnToFlowProcID"].ToString() != "")
                {
                    model.FReturnToFlowProcID = decimal.Parse(row["FReturnToFlowProcID"].ToString());
                }
                if (row["FNote"] != null)
                {
                    model.FNote = row["FNote"].ToString();
                }
                if (row["FDeptNo"] != null)
                {
                    model.FDeptNo = row["FDeptNo"].ToString();
                }
                if (row["FEmplNo"] != null)
                {
                    model.FEmplNo = row["FEmplNo"].ToString();
                }
                if (row["FProcDate"] != null && row["FProcDate"].ToString() != "")
                {
                    model.FProcDate = DateTime.Parse(row["FProcDate"].ToString());
                }
                if (row["FInsIP"] != null)
                {
                    model.FInsIP = row["FInsIP"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FFlowProcID,FEntityNo,FTableName,FSourceNo,FSeq,FStatus,FReturnToFlowProcID,FNote,FDeptNo,FEmplNo,FProcDate,FInsIP ");
            strSql.Append(" FROM fund.t_WorkFlowProcess ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FFlowProcID,FEntityNo,FTableName,FSourceNo,FSeq,FStatus,FReturnToFlowProcID,FNote,FDeptNo,FEmplNo,FProcDate,FInsIP ");
            strSql.Append(" FROM fund.t_WorkFlowProcess ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM fund.t_WorkFlowProcess ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FFlowProcID desc");
            }
            strSql.Append(")AS Row, T.*  from fund.t_WorkFlowProcess T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_WorkFlowProcess";
            parameters[1].Value = "FFlowProcID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

